﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="AbstractCameraManager.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2011
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Render.
//
//   starLiGHT.Render is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Core is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Render. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Render are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 5425                  $:  Revision of last commit *
// * $Author:: unknown            $:  Author of last commit   *
// * $Date:: 2011-02-18 15:38:38 #$:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.Render.Camera
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using starLiGHT;
    using starLiGHT.Collections;

    #endregion

    /// <summary>
    /// Provides basic functionality for adding, removing and applying effects to cameras in the game. Actual camera managers should derive from this abstract class.
    /// </summary>
    public abstract class AbstractCameraManager
    {
        #region Member Variables
        private GraphicsDevice graphicsDevice;
        private Dictionary<string, ICamera> cameras;
        private ICamera activeCamera;

        #endregion

        /// <summary>
        /// Initializes a new instance of the AbstractCameraManager class.
        /// </summary>
        /// <param name="graphicsDevice">GraphicsDevice the manager uses for the cameras.</param>
        public AbstractCameraManager(GraphicsDevice graphicsDevice)
        {
            this.graphicsDevice = graphicsDevice;
            this.cameras = new Dictionary<string, ICamera>();
        }

        /// <summary>
        /// Gets the currently active camera.
        /// </summary>
        public ICamera ActiveCamera
        {
            get { return this.activeCamera; }
        }

        /// <summary>
        /// Gets the GraphicsDevice this manager uses.
        /// </summary>
        public GraphicsDevice GraphicsDevice
        {
            get
            {
                return this.graphicsDevice;
            }
        }

        /// <summary>
        /// Adds a camera to the manager.
        /// </summary>
        /// <param name="name">Name of the camera to add.</param>
        /// <param name="camera">Camera to add to the manager.</param>
        public void AddCamera(string name, ICamera camera)
        {
            this.AddCamera(name, camera, false);
        }

        /// <summary>
        /// Adds a camera to the manager.
        /// </summary>
        /// <param name="name">Name of the camera to add.</param>
        /// <param name="camera">Camera to add to the manager.</param>
        /// <param name="makeActive">Whether or not the camera should be the active camera.</param>
        public void AddCamera(string name, ICamera camera, bool makeActive)
        {
            this.cameras.Add(name, camera);

            if (makeActive)
            {
                this.activeCamera = camera;
            }
        }

        /// <summary>
        /// Determines if a camera with the specified name exists within the manager.
        /// </summary>
        /// <param name="name">Name of the camera to search for.</param>
        /// <returns>Returns TRUE if the manager contains a camera with the specified name. Returns FALSE otherwise.</returns>
        public bool HasCamera(string name)
        {
            return this.cameras.ContainsKey(name);
        }

        /// <summary>
        /// Returns TRUE if the manager contains a specific camera.
        /// </summary>
        /// <param name="camera">Camera to search for.</param>
        /// <returns>Returns TRUE if the manager contains the specified camera. Returns FALSE otherwise.</returns>
        public bool HasCamera(ICamera camera)
        {
            return this.cameras.ContainsValue(camera);
        }

        /// <summary>
        /// Removes a camera from the manager.
        /// </summary>
        /// <param name="name">Name of the camera to remove.</param>
        public void RemoveCamera(string name)
        {
            ICamera cameraToRemove;

            if (this.cameras.TryGetValue(name, out cameraToRemove))
            {
                this.cameras.Remove(name);

                if (this.activeCamera.Equals(cameraToRemove))
                {
                    this.activeCamera = null;
                }
            }
        }

        /// <summary>
        /// Makes a specific camera the active camera.
        /// </summary>
        /// <param name="camera">Camera to activate.</param>
        public void SetActiveCamera(ICamera camera)
        {
            this.activeCamera = camera;
        }

        /// <summary>
        /// Makes a camera of the specified name the active camera.
        /// </summary>
        /// <param name="name">Name of the camera to activate.</param>
        public void SetActiveCamera(string name)
        {
            ICamera cameraToSet;

            if (this.cameras.TryGetValue(name, out cameraToSet))
            {
                this.activeCamera = cameraToSet;
            }
        }

#if !ZUNE
        /// <summary>
        /// Applies an Effect's Parameters to the manager.
        /// </summary>
        /// <param name="effect">Effect of which Parameters will be applied to the manager.</param>
        public abstract void ApplyCameraEffectParameters(ref Effect effect);
#endif

        /// <summary>
        /// Updates the manager. The client needs to call this function in order to update the managed cameras.
        /// </summary>
        /// <param name="gameTime">Provides snapshots of timing values.</param>
        public abstract void Update(GameTime gameTime);
    }
}
